package com.kede.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kede.PubConst;
import com.kede.dictenum.BoolDictEnum;
import com.kede.domain.UnishopProductDomain;
import com.kede.domain.ZhiCategoryDomain;
import com.kede.domain.ZhiOrderGoodsListDomain;
import com.kede.ienum.zhi.ZhiOrderStatusEnum;
import com.kede.mapper.UnishopProductMapper;
import com.kede.pojo.GoodPackageStatus;
import com.kede.pojo.PagePO;
import com.kede.pojo.ResHttp;
import com.kede.pojo.ShopProduct;
import com.kede.service.UnishopProductService;
import com.kede.service.ZhiCategoryService;
import com.kede.service.ZhiOrderGoodsListService;
import com.kede.utils.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


/**
* @author 11856
* @description 针对表【sy_unishop_product(产品表)】的数据库操作Service实现
* @createDate 2023-06-17 12:30:56
*/
@Slf4j
@Service
public class UnishopProductServiceImpl extends ServiceImpl<UnishopProductMapper, UnishopProductDomain>
    implements UnishopProductService{

    @Resource
    private ZhiOrderGoodsListService zhiOrderGoodsListService;

    @Resource
    private ZhiCategoryService zhiCategoryService;

    @Override
    public List<ShopProduct> getZhiCateGood() {
        //php 之前写死的积分分类
        LambdaQueryWrapper<UnishopProductDomain> query = new LambdaQueryWrapper<>();
        query.eq(UnishopProductDomain::getSwitchStatus, BoolDictEnum.TRUE.getCode());
        List<UnishopProductDomain> list = list(query);
        List<ShopProduct> shopProducts = BeanUtil.copyToList(list, ShopProduct.class);
        //java 灵活的现金分类
         LambdaQueryWrapper<ZhiCategoryDomain> moneyCate = new LambdaQueryWrapper<>();
        moneyCate.eq(ZhiCategoryDomain::getPid, PubConst.ZERO);
        moneyCate.eq(ZhiCategoryDomain::getIsActive,BoolDictEnum.TRUE.getCode());
        List<ShopProduct> moneyCateList = zhiCategoryService.list(moneyCate).stream().map(zhiCategoryDomain -> {
            return ShopProduct.builder().image("").type(PubConst.MONEY_CATE).count(0).title(zhiCategoryDomain.getName()).build();
        }).collect(Collectors.toList());
        shopProducts.addAll(moneyCateList);

        return shopProducts.stream().distinct().collect(Collectors.toList());
    }

    @Override
    public ResHttp getMyGoods(PagePO pagepo,Integer storeUserId, String cateName, String keyword) {
        List<OrderItem> orders = pagepo.orders();
        if (CollectionUtil.Blank(orders)){
            orders.add(OrderItem.desc("createtime"));
        }
        LambdaQueryWrapper<ZhiOrderGoodsListDomain> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZhiOrderGoodsListDomain::getStoreId,storeUserId);
        queryWrapper.eq(ZhiOrderGoodsListDomain::getStatus, ZhiOrderStatusEnum.FINISH.getCode());
        //没卖出去的
//        queryWrapper.eq(ZhiOrderGoodsListDomain::getIsSell,BoolDictEnum.FALSE.getCode());
        queryWrapper.eq(ZhiOrderGoodsListDomain::getGoodPackageStatus,GoodPackageStatus.UnPackage.getStatus());
        if (StrUtil.isNotBlank(cateName)){
            queryWrapper.eq(ZhiOrderGoodsListDomain::getCateName,cateName);
        }
        if (StrUtil.isNotBlank(keyword)){
            queryWrapper.like(ZhiOrderGoodsListDomain::getMessage,keyword);
        }
        PagePO<ZhiOrderGoodsListDomain> page = zhiOrderGoodsListService.page(pagepo, queryWrapper);
        return ResHttp.okData(page);
    }

    /**
     * 获取我的物品的各类统计数
     * @param storeUserId
     * @return
     */
    @Override
    public List<ShopProduct> getMyGoodNum(Integer storeUserId){
        List<ShopProduct> zhiCateGood = getZhiCateGood();
        zhiCateGood.forEach(shopProduct -> {
            LambdaQueryWrapper<ZhiOrderGoodsListDomain> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ZhiOrderGoodsListDomain::getStoreId,storeUserId);
            queryWrapper.eq(ZhiOrderGoodsListDomain::getStatus, ZhiOrderStatusEnum.FINISH.getCode());
//            queryWrapper.eq(ZhiOrderGoodsListDomain::getIsSell,BoolDictEnum.FALSE.getCode());
            queryWrapper.eq(ZhiOrderGoodsListDomain::getGoodPackageStatus, GoodPackageStatus.UnPackage.getStatus());
            queryWrapper.eq(ZhiOrderGoodsListDomain::getCateName,shopProduct.getTitle().trim());
            int count = zhiOrderGoodsListService.count(queryWrapper);
            shopProduct.setCount(count);
        });
        return zhiCateGood;
    }


}




